package com.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.GradeBean;
import com.project.bean.RoleBean;
import com.project.mapper.IGradeMapper;
import com.project.mapper.IRoleMapper;
import com.project.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private IRoleMapper roleMapper;

    @Override
    public String findByRoleIdArray(Integer[] roleIdArray) {
        if (roleIdArray.length == 0) {
            return null;
        }
        List<GradeBean> gradeList = roleMapper.findByRoleIdArray(roleIdArray);
        String str = "";
        for (int i = 0; i < gradeList.size(); i++) {
            str += gradeList.get(i).getGradeName() + "、";
        }
        String gradeStr = str.substring(0, str.length() - 1);

        return gradeStr;
    }

    @Override
    public List<RoleBean> findRoleListByUserId(Integer userId) {
        return roleMapper.findRoleListByUserId(userId);
    }

    @Override
    public IPage<RoleBean> cutByItem(Integer pageNO) {
        return roleMapper.selectPage(new Page<>(pageNO, 3), null);
    }

    @Override
    public List<RoleBean> findAllRole() {
        return roleMapper.selectList(null);
    }

    @Override
    public void addRole(RoleBean role, Integer[] gradeIdArray) {
        roleMapper.insert(role);
        roleMapper.updateGrade(role.getRoleId(), gradeIdArray);
    }

    @Override
    public void delRole(Integer roleId) {
        roleMapper.delRole(roleId);
    }

    @Override
    public RoleBean findByRoleId(Integer roleId) {
        return roleMapper.findByRoleId(roleId);
    }

    @Override
    public void updateGrade(Integer roleId, Integer[] newGradeIdArray) {
        roleMapper.updateGrade(roleId, newGradeIdArray);
    }

    @Override
    public void delByUser(Integer userId) {
        roleMapper.delByUser(userId);
    }
}
